# -*- coding: utf-8 -*-
# @Time : 2025/6/24 下午3:15
# @Author : cc 
# @Email : liuchenchen@starmerx.com
# @Project : my-tools
# @Desc : 功能描述
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow
from PyQt5.QtCore import QTimer, Qt
from PyQt5.QtGui import QPainter, QColor, QLinearGradient


class GradientScanner(QLabel):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.scan_progress = 0
        self.setMinimumSize(400, 400)

    def paintEvent(self, event):
        super().paintEvent(event)
        painter = QPainter(self)

        # 创建渐变效果
        gradient = QLinearGradient(0, self.scan_progress, 0, self.scan_progress + 30)
        gradient.setColorAt(0, QColor(0, 255, 0, 0))
        gradient.setColorAt(0.5, QColor(0, 255, 0, 150))
        gradient.setColorAt(1, QColor(0, 255, 0, 0))

        painter.fillRect(0, self.scan_progress, self.width(), 30, gradient)


class GradientScannerWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("渐变扫描动画")

        self.scanner = GradientScanner()
        self.scanner.setText("模拟文档扫描\n\n渐变扫描效果")
        self.scanner.setAlignment(Qt.AlignCenter)
        self.scanner.setStyleSheet("background-color: #222; color: white; font-size: 18px;")
        self.setCentralWidget(self.scanner)

        # 定时器动画
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.animate_scan)
        self.timer.start(30)  # 30ms更新一次

        self.direction = 1  # 1向下，-1向上

    def animate_scan(self):
        self.scanner.scan_progress += 2 * self.direction

        # 到达边界时反转方向
        if self.scanner.scan_progress >= self.scanner.height():
            self.direction = -1
        elif self.scanner.scan_progress <= 0:
            self.direction = 1

        self.scanner.update()


if __name__ == "__main__":
    app = QApplication([])
    window = GradientScannerWindow()
    window.resize(500, 500)
    window.show()
    app.exec_()